*******************************************************************************
* Load the candidate data
*******************************************************************************

use dataset_candidates, clear
drop if mi(ideology)

*******************************************************************************
* Get n unique districts by state
*******************************************************************************

egen UniqueDistricts = nvals(district), by(state)

*******************************************************************************
* Collapse by state
*******************************************************************************

collapse Districts WithContested CandidatesSampled ///
	UniqueDistricts, by(state)
order state D With Unique CandidatesSampled   
	
*******************************************************************************
* Totals
*******************************************************************************

insobs 1
list
replace state = "Total" if state == ""
foreach x in D WithC Unique CandidatesSampled {
	total `x'
	ereturn list
	mat list e(b)
	replace `x' = e(b)[1,1] if mi(`x')
}

*******************************************************************************
* Output table
*******************************************************************************

#delimit;

listtex 
	using "_output/table1.tex"
	,
	replace
	type rstyle(tabular)
	head(
		\begin{tabular*}
		{\hsize}{@{\hskip\tabcolsep\extracolsep\fill}l*{5}{c}}
		\toprule 
		 State & Districts & w/ Competition & 
		 Sampled Districts & Sampled Candidates
		 \\		 
		\midrule
	)
	foot("\bottomrule\end{tabular*}")
	;
	
#delimit cr

*******************************************************************************
* End
*******************************************************************************
